package com.pie.face_pie.service;

import java.sql.Blob;

import com.pie.face_pie.mapper.RegisterMapper;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import javax.sql.rowset.serial.SerialBlob;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.sql.SQLException;


@Component
public class RateService {
    @Autowired
    DetectService detectService;

    @Autowired
    RabbitTemplate rabbitTemplate;

    @Autowired
    SendService sendService;

    public int rate(MultipartFile[] files, String id) throws IOException, SQLException {
        int len = files.length;
        int count = 0;
        for (int i = 0; i < len; i++) {
            int result = detectService.detect(files[i]);
            System.out.println(result);
            if (result == 1){
                count ++;   //表示检测到一张人脸
            }else {
                files[i] = null;
            }
        }

        if (len/count < 2){
            InputStream[] inputStreams = new InputStream[len];
            for (int i = 0; i < len; i++) {
                if (files[i] != null){
                    inputStreams[i] = files[i].getInputStream();
                }
            }
            sendService.send(inputStreams,id);
            System.out.println("a");
            return 1;   //表示注册成功
        }
        return 0; //表示注册失败(太多张没有对准)
    }

}
